import time
import serial
import pyautogui
ser = serial.Serial("/dev/ttyUSB0", 9600, timeout=5)
ser.flushInput()
pyautogui.FAILSAFE = False
error = 0
while True:
    try:
        count = ser.inWaiting()
        if not count == 0:
            recv = ser.read(ser.in_waiting).decode("utf-8")
            print(time.time(), "-->", recv)
            if str(recv).replace(" ", "").replace("\n", "")[0] == "0":
                #keyboard.press("left")
                pyautogui.press("up")
            elif str(recv).replace(" ", "").replace("\n", "")[0] == "1":
                pyautogui.press("down")
    except:
        error = error + 1
        print("\rError X{}".format(str(error)), end=" ")
        time.sleep(0.1)
    time.sleep(0.1)
    

